﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.Lib;

namespace UI.ShopManagement
{
    public partial class FrmBas1a01Edit : UI.ParentForm.BaseEdit
    {
        public FrmBas1a01Edit(string action, string text, string key, string[] fj)
            : base(action)
        {
            InitializeComponent();

            this.Text = text;
            this.key = key;
            //回写的方法
            this.SaveCallFuntion = "Bas1a01Manager$SaveData"; //保存的方法
            if ("A" == this.Action)
            {
                IsAfterSaveCloseWin = false;
            }
            else
            {
                LoadData();
            }

            if (null != fj) //右键菜单进入
            {
                this.tbSummary.SetValue("plistid", fj[0]); //父级代码主键
                this.tbSummary.SetValue("pcode", fj[1]); //父级代码
                this.tbSummary.SetValue("pname", fj[2]); //父级代码
            }

            this.Size = base.LstWindow;
        }

        protected override bool Check()
        {
            if ("A" == this.Action)
            {
                System.Collections.Hashtable hs = new System.Collections.Hashtable();
                hs["areacode"] = this.tbSummary.GetValue("areacode");
                hs["%muser"] = UI.Lib.Context.GetAppKeyWhere();
                bool reflt = UI.Lib.Factory.CommonHelper.Entity.DataInData("Bas1a01", hs);
                if (reflt)
                {
                    MessageBox.Show("当前区域代码已经存在！", "提示");
                    this.tbSummary.Focus("areacode");
                    return false;
                }

                return true;
            }

            return true;
        }

        public override bool BeforePopWin(string name, ref string where, ref string[] others)
        {
            //限制父级区域代码的取值范围
            if ("pcode" == name)
            {
                if ("M" == this.Action)
                {
                    where = string.Format(" NOT EXISTS(SELECT 1 FROM dbo.getchilds_mclass_fun('{0}') "
                    + " WHERE a.listid = alistid) AND A.listid <> '{0}' AND {1}", this.tbSummary.GetValue("listid")
                        , Context.GetAppKeyWhere("muser"));
                }
                else if ("A" == this.Action)
                {
                    where = Context.GetAppKeyWhere("muser");
                }
            }
            return true;
        }

        //加载修改数据
        private void LoadData()
        {
            base.LoadEditData(null, string.Format(" a.listid ='{0}' ", this.key));
        }
    }
}